package com.jh.fcsm.controller.sys;

import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.SysTableConfig;
import com.jh.fcsm.beans.sys.vo.SysTableConfigVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.service.sys.SysTableConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表格基础配置Controller
 *
 * @author linq
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/sys/tableconfig")
@Api(value = "表格基础配置", description = "表格基础配置")
public class SysTableConfigController extends BaseController {
    @Autowired
    private SysTableConfigService sysTableConfigService;

    private static final String PER_PREFIX = "btn:sys:tableconfig:";

    /**
     * 新增表格基础配置
     *
     * @param sysTableConfig 表格基础配置数据 json
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/save")
    @ApiOperation(value = "新增表格基础配置", notes = "新增表格基础配置")
    @SystemLogAnnotation(type = "表格基础配置", value = "新增表格基础配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "save')")
    public RestApiResponse<?> saveSysTableConfig(@RequestBody SysTableConfig sysTableConfig) {
        String id = sysTableConfigService.saveOrUpdateSysTableConfig(sysTableConfig);
        return RestApiResponse.ok(id);
    }

    /**
     * 修改表格基础配置
     *
     * @param sysTableConfig 表格基础配置数据 json
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/update")
    @ApiOperation(value = "修改表格基础配置", notes = "修改表格基础配置")
    @SystemLogAnnotation(type = "表格基础配置", value = "修改表格基础配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "update')")
    public RestApiResponse<?> updateSysTableConfig(@RequestBody SysTableConfig sysTableConfig) {
        String id = sysTableConfigService.saveOrUpdateSysTableConfig(sysTableConfig);
        return RestApiResponse.ok(id);
    }

    /**
     * 批量删除表格基础配置(判断 关联数据是否可以删除)
     *
     * @param ids
     * @return RestApiResponse<?>
     * @author linq
     */
    @RepeatSubAnnotation
    @PostMapping("/delete")
    @ApiOperation(value = "批量删除表格基础配置", notes = "批量删除表格基础配置")
    @SystemLogAnnotation(type = "表格基础配置", value = "批量删除表格基础配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "delete')")
    public RestApiResponse<?> deleteSysTableConfig(@RequestBody List<String> ids) {
        //判断 关联数据是否可以删除
        sysTableConfigService.deleteSysTableConfig(ids);
        return RestApiResponse.ok();
    }

    /**
     * 查询表格基础配置详情
     *
     * @param id
     * @return RestApiResponse<?>
     * @author linq
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询表格基础配置详情", notes = "查询表格基础配置详情")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam("id") String id) {
        SysTableConfig sysTableConfig = sysTableConfigService.findById(id);
        return RestApiResponse.ok(sysTableConfig);
    }

    /**
     * 分页查询表格基础配置
     *
     * @param sysTableConfigVo 表格基础配置 查询条件
     * @return RestApiResponse<?>
     * @author linq
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询表格基础配置", notes = "分页查询表格基础配置")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@RequestBody SysTableConfigVo sysTableConfigVo) {
        PageInfo<SysTableConfig> sysTableConfig = sysTableConfigService.findPageByQuery(sysTableConfigVo);
        return RestApiResponse.ok(sysTableConfig);
    }


    @GetMapping("/findByFlag")
    @ApiOperation(value = "查询表格基础配置详情", notes = "查询表格基础配置详情")
    //@PreAuthorize("hasAuthority('"+PER_PREFIX+"findByFlag')")
    public RestApiResponse<?> findByFlag(@RequestParam("tableFlag") String tableFlag) {
        JSONArray sysTableConfig = sysTableConfigService.findExcelJsonByFlag(tableFlag);
        return RestApiResponse.ok(sysTableConfig);
    }
}
